<?php

namespace app\common\Attribute;

use app\common\enum\HttpMethod;

/**
 * 定义请求方法属性
 * 
 * @Attribute
 */
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
class RequestMethod
{
    /**
     * 支持的 HTTP 请求方法列表
     * @var array<string>
     */
    public array $methods;

    public function __construct(HttpMethod|array $method)
    {
        if ($method instanceof HttpMethod) {
            $this->methods = [$method->value];
        } else {
            $values = HttpMethod::values();
            $this->methods = array_map(
                fn(HttpMethod $m) => $m->value,
                array_filter(
                    $method,
                    fn(HttpMethod $m) => in_array($m->value, $values, true)
                )
            );

            if (count($this->methods) !== count($method)) {
                throw new \InvalidArgumentException('存在不支持的 HTTP 方法');
            }
        }
    }
}